// use std::env;
// use std::fs::File;
use std::io::{ Result};
// use std::path::PathBuf;
use calamine::{open_workbook_auto, DataType,  Reader};
// use glob::{glob, GlobError, GlobResult};
// use regex::Regex;
use crate::common::Loc;

// #[derive(Debug)]
// enum FileStatus {ff
//     VbaError(Error),
//     RangeError(Error),
//     Glob(GlobError),
// }



pub fn handle_lines<F>(name: &str,sheet_names:&str, each : F, mut loc : Loc) -> Result<()>
where F:Fn(i32, &[DataType],&Loc)
{
    let mut workbook = open_workbook_auto(name).expect("Cannot open file");
    let names = workbook.sheet_names();
    let sh_name = if sheet_names == ""{
        names[0].clone()
    }else{
        sheet_names.to_owned()
    };
    
    if let Some(Ok(range)) = workbook.worksheet_range(&sh_name) {
        loc.le  = range.get_size().0 as i32;
        // loc.le = range.get_size().1 as i32;
        // println!("{} {} {:?}", name, sh_name,loc);
    
        for (ix, row) in range.rows().enumerate() {
            each(ix as i32, row, &loc);
        }
        // atively, we can manually filter rows
        // assert_eq!(non_empty_cells, range.rows()
            // .flat_map(|r| r.iter().filter(|&c| c != &DataType::Empty)).count());
    }
    
    Ok(())
}

pub fn oper(no:i32, values: &[DataType], loc:&Loc) {
    if no >= loc.ls && (no < loc.le || loc.le == -1){
        if match loc.mode{
            crate::MODE_GREEP_BY => {
                if let Some(_) = loc.grep_by_vec.iter().find(|&compare_line| {
                    format!("{:?}",values).contains(compare_line)
                }){
                    true
                }else{
                    false
                }
            },
            _ => {
                true
            }
        }{
            loc.show_line_or_values(crate::common::Value::Multi(values));
        }
    }
}